package com.hrg.demo.util;
import java.io.*;
import java.nio.file.*;

/**
 * @author shenzq
 */
public class ImageProcessor {

    public static String renameImageFile(String originalImagePath, String newName) throws IOException {
        Path sourcePath = Paths.get(originalImagePath);

        if (!Files.exists(sourcePath)) {
            throw new FileNotFoundException("图片文件不存在: " + originalImagePath);
        }

        // 获取原文件信息
        String originalFileName = sourcePath.getFileName().toString();
        String fileExtension = getFileExtension(originalFileName);

        // 构建新文件路径（同一目录下）
        Path targetPath = sourcePath.getParent().resolve(newName + "." + fileExtension);

        // 复制文件（如果要在原位置重命名，使用 Files.move）
        Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);

        return targetPath.toString();
    }

    private static String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
    }
}
